// pages/medical-record-delivery/record-selection.js
Page({
	data: {
		patientInfo: {
			name: '蒋峰',
			hospitalizationNumber: '400909',
			admissionDate: '2020/10/09',
			dischargeDate: '2020/10/09',
			status: '已出院'
		},
		recordTypes: [
			{ id: 1, name: '病案首页', price: 2, quantity: 2 },
			{ id: 2, name: '出院记录', price: 3, quantity: 1 },
			{ id: 3, name: '入院记录', price: 3, quantity: 1 },
			{ id: 4, name: '手术记录', price: 1, quantity: 0 },
			{ id: 5, name: '麻醉记录', price: 1, quantity: 0 },
			{ id: 6, name: '病理报告', price: 1, quantity: 0 },
			{ id: 7, name: '化验报告', price: 1, quantity: 0 },
			{ id: 8, name: '医嘱', price: 4, quantity: 0 }
		],
		totalAmount: 10.00
	},

	onLoad(options) {
		console.log('病历选择页面加载')
		this.loadPatientInfo()
		this.calculateTotal()
	},

	// 加载患者信息
	loadPatientInfo() {
		// 获取之前保存的表单数据
		const formData = wx.getStorageSync('medicalRecordFormData')
		if (formData && formData.patientName) {
			this.setData({
				patientInfo: {
					...this.data.patientInfo,
					name: formData.patientName,
					hospitalizationNumber: formData.hospitalizationNumber || '400909'
				}
			})
		}

		// 从服务器获取患者详细信息
		wx.request({
			url: `http://localhost:8080/patient/info/${this.data.patientInfo.hospitalizationNumber}`,
			method: 'GET',
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				if (res.statusCode === 200 && res.data) {
					const patientData = res.data
					this.setData({
						patientInfo: {
							...this.data.patientInfo,
							admissionDate: patientData.admissionDate || '2020/10/09',
							dischargeDate: patientData.dischargeDate || '2020/10/09',
							status: patientData.status || '已出院'
						}
					})
				}
			},
			fail: (error) => {
				console.error('获取患者信息失败:', error)
			}
		})
	},

	// 增加数量
	increaseQuantity(e) {
		const id = e.currentTarget.dataset.id
		const recordTypes = this.data.recordTypes.map(item => {
			if (item.id === id) {
				return { ...item, quantity: item.quantity + 1 }
			}
			return item
		})
		
		this.setData({
			recordTypes: recordTypes
		})
		
		this.calculateTotal()
	},

	// 减少数量
	decreaseQuantity(e) {
		const id = e.currentTarget.dataset.id
		const recordTypes = this.data.recordTypes.map(item => {
			if (item.id === id && item.quantity > 0) {
				return { ...item, quantity: item.quantity - 1 }
			}
			return item
		})
		
		this.setData({
			recordTypes: recordTypes
		})
		
		this.calculateTotal()
	},

	// 计算总金额
	calculateTotal() {
		const total = this.data.recordTypes.reduce((sum, item) => {
			return sum + (item.price * item.quantity)
		}, 0)
		
		this.setData({
			totalAmount: total.toFixed(2)
		})
	},

	// 结算
	checkout() {
		// 检查是否有选择内容
		const hasSelection = this.data.recordTypes.some(item => item.quantity > 0)
		if (!hasSelection) {
			wx.showToast({
				title: '请选择复印内容',
				icon: 'none'
			})
			return
		}

		// 保存选择的数据
		const formData = wx.getStorageSync('medicalRecordFormData') || {}
		formData.selectedRecords = this.data.recordTypes.filter(item => item.quantity > 0)
		formData.totalAmount = this.data.totalAmount
		wx.setStorageSync('medicalRecordFormData', formData)

		// 跳转到支付页面
		wx.navigateTo({
			url: '/pages/medical-record-delivery/payment'
		})
	}
}) 